/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Telas;

import Gerenciar.GerenciarParceria;
import entidades.Parceria;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Deyves
 */
public class ConsultaParceria extends javax.swing.JDialog {

    private List<Parceria> lista = GerenciarParceria.trazerParceria();

    public ConsultaParceria(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        btnLocalizar.setVisible(false);
        atualizarTabelaParceria();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtLocalizar = new javax.swing.JTextField();
        btnLocalizar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblConsultaParceria = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        txtLocalizar.setFont(new java.awt.Font("Arial", 2, 14)); // NOI18N
        txtLocalizar.setText("Digite aqui");
        txtLocalizar.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                txtLocalizarComponentAdded(evt);
            }
        });
        txtLocalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtLocalizarActionPerformed(evt);
            }
        });
        txtLocalizar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtLocalizarFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtLocalizarFocusLost(evt);
            }
        });
        txtLocalizar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtLocalizarKeyPressed(evt);
            }
        });

        btnLocalizar.setText("Localizar");
        btnLocalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLocalizarActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        tblConsultaParceria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "Nome", "CNPJ", "Telefone", "Endereço"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblConsultaParceria);
        tblConsultaParceria.getColumnModel().getColumn(0).setMinWidth(0);
        tblConsultaParceria.getColumnModel().getColumn(0).setMaxWidth(0);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(txtLocalizar, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLocalizar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtLocalizar, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(btnLocalizar)
                    .addComponent(btnEditar)
                    .addComponent(btnCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txtLocalizarComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_txtLocalizarComponentAdded
        System.out.println("a");
    }//GEN-LAST:event_txtLocalizarComponentAdded

    private void txtLocalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtLocalizarActionPerformed
    }//GEN-LAST:event_txtLocalizarActionPerformed

    private void txtLocalizarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtLocalizarFocusGained
        txtLocalizar.setText("");
    }//GEN-LAST:event_txtLocalizarFocusGained

    private void txtLocalizarFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtLocalizarFocusLost
        if (txtLocalizar.getText().trim().equalsIgnoreCase("")) {
            txtLocalizar.setText("Digite aqui");
        }
    }//GEN-LAST:event_txtLocalizarFocusLost

    private void txtLocalizarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLocalizarKeyPressed
        for (int i = 0; i <= tblConsultaParceria.getRowCount() - 1; i++) {
            if (((DefaultTableModel) tblConsultaParceria.getModel()).getValueAt(i, 1).toString().contains(txtLocalizar.getText().toUpperCase())) {
                ((DefaultTableModel) tblConsultaParceria.getModel()).getValueAt(i, 1);
                tblConsultaParceria.changeSelection(i, 1, rootPaneCheckingEnabled, rootPaneCheckingEnabled);
            } else {
//                JOptionPane.showMessageDialog(null, "Não Encontrado!");
//                txtLocalizar.setText("Digite aqui");
            }
        }
    }//GEN-LAST:event_txtLocalizarKeyPressed

    private void btnLocalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLocalizarActionPerformed
        //       for (int i = 0; i <= tblConsultaCliente.getRowCount()-1; i++) {
        //            if (((DefaultTableModel) tblConsultaCliente.getModel()).getValueAt(i, 1).toString().contains(txtLocalizar.getText().toUpperCase())) {
        //                ((DefaultTableModel) tblConsultaCliente.getModel()).getValueAt(i, 1);
        //                tblConsultaCliente.changeSelection(i, 1, closable, isIcon);
        //            }else{
        //                JOptionPane.showMessageDialog(null, "Não Encontrado!");
        //                txtLocalizar.setText("Digite aqui");
        //            }
        //        }
    }//GEN-LAST:event_btnLocalizarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        if (tblConsultaParceria.getSelectedRow() >= 0) {  //----------- CONDIÇÃO EM QUE SE TIVER UMA LINHA SELECIONA, ENTAO EXECUTA
            int id = (int) tblConsultaParceria.getValueAt(tblConsultaParceria.getSelectedRow(), 0);
            Parceria parceria = GerenciarParceria.trazerParceria(id);
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        txtLocalizar.setText("Digite aqui");
        tblConsultaParceria.clearSelection();
    }//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConsultaParceria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConsultaParceria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConsultaParceria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConsultaParceria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ConsultaParceria dialog = new ConsultaParceria(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnLocalizar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblConsultaParceria;
    private javax.swing.JTextField txtLocalizar;
    // End of variables declaration//GEN-END:variables

    private void atualizarTabelaParceria() { //--------- METODO DE ATUALIZAR TABELA
        limparTabela();
        //List<Cliente> lista = GerenciarCliente.trazerClientes();
        String juntaParceria;
        Object[] objectemp = new Object[5];
        for (int i = 0; i <= lista.size() - 1; i++) {
            objectemp[0] = lista.get(i).getId();
            juntaParceria = lista.get(i).getDescricao();
            objectemp[1] = juntaParceria.toUpperCase();
            juntaParceria = lista.get(i).getCnpj().toString();
            objectemp[2] = juntaParceria.toUpperCase();
            objectemp[3] = lista.get(i).getTelefoneparcerias().toString() + " / " + lista.get(i).getTelefoneparcerias_1().toString();
            juntaParceria = lista.get(i).getEnderecoparcerias().toString(); //+ ", "
//                    + lista.get(i).getEnderecocliente().getNumero().toString() + " - "
//                    + lista.get(i).getEnderecocliente().getBairro() + " / "
//                    + lista.get(i).getEnderecocliente().getCidade().getNome() + " - "
//                    + lista.get(i).getEnderecocliente().getCidade().getEstado().getDescricao();
            objectemp[4] = juntaParceria.toUpperCase();
            ((DefaultTableModel) tblConsultaParceria.getModel()).addRow(objectemp);
        }
    }

    private void limparTabela() { //---------- LIMPAR TABELA
        int numLinhas = ((DefaultTableModel) tblConsultaParceria.getModel()).getRowCount();
        for (int i = numLinhas - 1; i >= 0; i--) {
            ((DefaultTableModel) tblConsultaParceria.getModel()).removeRow(i);
        }
    }
}
